﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using MySoft.Data;
using System.Data;
using System.Data.SqlClient;


namespace SDLITS.Web.SystemManage
{
    public partial class DriverVisitLogManage : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txt_edate.SelectedDate = DateTime.Now;
            }
        }
        private void BindGrid()
        {
            DateTime? startDate = null;
            DateTime? endDate = null;
            int startTime = 0;
            int endTime = 0;
            WhereClip wc = WhereClip.All;

            if (!string.IsNullOrEmpty(txt_sdate.Text))
            {
                startDate = Convert.ToDateTime(txt_sdate.Text);

            }
            if (!string.IsNullOrEmpty(txt_edate.Text))
            {
                endDate = Convert.ToDateTime(txt_edate.Text);
            }
            if (txt_stime.SelectedDate != null)
            {
                startTime = Convert.ToDateTime(txt_stime.SelectedDate).Hour;
            }
            if (txt_etime.SelectedDate != null)
            {
                endTime = Convert.ToDateTime(txt_etime.SelectedDate).Hour;
            }

            SqlParameter[] sp = new SqlParameter[]{
            new SqlParameter("@MemCardNo",txtMemCardNo.Text.Trim()),
            new SqlParameter("@StartDate",startDate),
            new SqlParameter("@EndDate",endDate),
            new SqlParameter("@StartTime",startTime),
            new SqlParameter("@EndTime",endTime)
            };
            Grid1.Hidden = true;
            lblcards.Hidden = true;
            if (cbl_all.Checked)
            {
                if (txtMemCardNo.Text != "")
                    wc = wc && V_MemEnterLog._.MemCardNo == txtMemCardNo.Text;
                string sdate = "";
                if (startDate != null)
                {
                    sdate = startDate.Value.ToString("yyyy-MM-dd");
                    if (startTime > 0)
                        sdate += " " + startTime.ToString() + ":00";
                }
                if (sdate != "")
                    wc = wc && V_MemEnterLog._.EnterTime > sdate;


                string edate = "";
                if (endDate != null)
                {
                    edate = endDate.Value.ToString("yyyy-MM-dd");
                    if (endTime > 0)
                        edate += " " + endTime.ToString() + ":00";
                }
                if (edate != "")
                    wc = wc && V_MemEnterLog._.EnterTime < edate;

                Grid1.RecordCount = SDDbSession.Default.From<V_MemEnterLog>().Where(wc).Count();

                Grid1.DataSource = SDDbSession.Default.From<V_MemEnterLog>().Where(wc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).OrderBy(V_MemEnterLog._.EnterTime.Desc).ToList();
                Grid1.DataBind();



                Grid1.Hidden = false;
                //DataSet ds = SDDbSession.Default.FromProc("SelectByDateRange").AddParameter(sp).ToDataSet();
                //Grid1.DataSource = ds.Tables[0];
                //Grid1.DataBind();
                DataSet ds = SDDbSession.Default.FromProc("SelectCountByDateRange").AddParameter(sp).ToDataSet();
                Label1.Text =  ds.Tables[0].Rows[0][0].ToString();
                Label2.Text =  ds.Tables[1].Rows[0][0].ToString();
            }
            else
            {
                lblcards.Hidden = false;
                DataSet ds = SDDbSession.Default.FromProc("SelectCountByDateRange").AddParameter(sp).ToDataSet();
                lblcards.Text = "总共进入次数：" + ds.Tables[0].Rows[0][0].ToString() + "   总共进入卡数：" + ds.Tables[1].Rows[0][0].ToString();
            }
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

    }
}
